-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
AVR_HVSP = require "mod_avr_hvsp"

g_cpu_define = {
	{	-- ATtiny25, not verified
		Signature	= { 0x1E, 0x91, 0x08 },
		FuseLow		= 0x62,
		FuseHigh	= 0xDF,
		FuseExt		= 0xFF,
	},
	{	-- ATtiny45, not verified
		Signature	= { 0x1E, 0x92, 0x06 },
		FuseLow		= 0x62,
		FuseHigh	= 0xDF,
		FuseExt		= 0xFF,
	},
	{	-- ATtiny85, not verified
		Signature	= { 0x1E, 0x93, 0x0B },
		FuseLow		= 0x62,
		FuseHigh	= 0xDF,
		FuseExt		= 0xFF,
	},
	{	-- ATtiny13
		Signature	= { 0x1E, 0x90, 0x07 },
		FuseLow		= 0x6A,
		FuseHigh	= 0xFF,
		FuseExt		= 0,
	},
	{	-- ATtiny12, not verified
		Signature	= { 0x1E, 0x90, 0x04 },
		FuseLow		= 0x52,
		FuseHigh	= 0,
		FuseExt		= 0,
	},
	{	-- ATtiny11, not verified
		Signature	= { 0x1E, 0x90, 0x05 },
		FuseLow		= 0x1C,
		FuseHigh	= 0,
		FuseExt		= 0,
	}
}
-- -----------------------------------------------------------------------------
	if not AVR_HVSP.device_open() then
		print("AVR_HVSP.device_open fail")
		return
	end
	
	if not AVR_HVSP.io_init() then
		print("AVR_HVSP.io_init fail")
	end

	AVR_HVSP.enter_HVSP_mode()

	print("Read device Signature Bytes:")
	Target_Signature = {0, 0, 0}

	for i=1, #Target_Signature do
		Target_Signature[i] = AVR_HVSP.GetSignature(i-1)
		print(string.format("	Target_Signature[%d] = 0x%x", i, Target_Signature[i]))
	end
	
	success = false
	for i=1, #g_cpu_define do
		-- print("i = " .. i)
		cpu = g_cpu_define[i]
		match = true
		Signature = cpu["Signature"]
		if #Signature == #Target_Signature then
			for j=1, #Signature do
				if Signature[j] ~= Target_Signature[j] then
					match = false
					break
				end
			end
			if match then
				FuseLow   = cpu["FuseLow"]
				FuseHigh  = cpu["FuseHigh"]
				FuseExt   = cpu["FuseExt"]
				
				print(string.format("Erase chip ..."))
				AVR_HVSP.ChipErase()
				
				-- Write fuse
				if FuseLow ~= 0 then
					print(string.format("Write FuseLow to  : 0x%x", FuseLow))
					if not AVR_HVSP.WriteFuseLowBits(FuseLow) then
						print("WriteFuseLowBits() fail")
						break
					end
				end
		
				if FuseHigh ~= 0 then
					print(string.format("Write FuseHigh to : 0x%x", FuseHigh))
					if not AVR_HVSP.WriteFuseHighBits(FuseHigh) then
						print("WriteFuseHighBits() fail")
						break
					end
				end
		
				if FuseExt ~= 0 then
					print(string.format("Write FuseExt to = 0x%x", FuseExt))
					if not AVR_HVSP.WriteFuseExtendedBits(FuseExt) then
						print("WriteFuseExtendedBits() fail")
						break
					end
				end

				success = true
				print("..........................")
				print(". Fuse Restore Successed .")
				print("..........................")
				--exit for loop
				break
			else
				-- print("Not matched ...")
			end
		else
			print("Signature Bytes not equal, #Signature = ".. #Signature ..", #Target_Signature:" ..  #Target_Signature)
		end
	end

	if not success then
		print("#############################")
		print("#    Fuse Restore Failed    #")
		print("#############################")
	end

	print("Exit HVSP mode")
	AVR_HVSP.exit_HVSP_mode()

	if not AVR_HVSP.device_close() then
		print("AVR_HVSP.device_close fail")
	end